
// 人员排班时调用的方法
function getTotalEchartsDataForArrange1(resData){
    console.log(resData)

    var startTime = "2019-07-01 00:00:00",endTime = "2019-07-31 00:00:00";

    var yData = formatterDayByDay1(startTime, endTime);
    console.log(yData)
    var sDaysList = [];
    for(var i = 0; i < resData.length;i++){
        sDaysList.push(getDay1(resData[i].sDays%7));
    }
    var units = resData[0].units;
    var cycle = resData[0].cycle;
    var runEndTime =  endTime + '.0';
    var runStartTime =  startTime + '.0';
    var timeRunStart = new Date(runStartTime.split(' ')[0]+' 00:00:00').getTime();
    var timeRunEnd = new Date(runEndTime.split(' ')[0]+' 00:00:00').getTime();


    var time1 = new Date(startTime).getTime();
    var time2 = new Date(runStartTime.split(' ')[0] + ' 00:00:00').getTime();
    var time3 = new Date(endTime).getTime();
    var time4 = new Date(runEndTime.split(' ')[0] + ' 00:00:00').getTime();
    var diffStart =  time1 - time2;//开始时间与当前班次的开始时间差
    var timeStartToEnd = time1 - time4;//开始时间与当前班次结束时间差
    var timeEndToEnd = time3 - time4;//结束时间与当前班次结束时间差
    var timeEndToStart = time3 - time2;//结束时间与当前班次开始时间差

    if(new Date(runEndTime.split(' ')[0]+' 00:00:00').getTime() >= new Date(endTime).getTime()){
        var endTimeStamp = endTime.split(' ')[0] + ' '+getMyDay1(endTime.split(' ')[0]);
    }else{
        var endTimeStamp = runEndTime.split(' ')[0] + ' '+getMyDay1(runEndTime.split(' ')[0]);
    }
    var diffEnd = yData.indexOf(endTimeStamp);
    for (var i = 0; i < resData.length; i++) {

        var startTimeString = formatTime1(resData[i].startTime).split(' ')[0];
        startTimeString = startTimeString + ' '+getMyDay1(startTimeString);
        //格式化临时排班的sDays
        if(yData.indexOf(startTimeString) >= 0 && resData[i].deilType !== 1){
            // sDaysArr.push(yData.indexOf(startTime));
            resData[i].sDays = yData.indexOf(startTimeString);
        }else if(yData.indexOf(startTimeString) < 0 && resData[i].deilType === 2){
            resData[i].sDays = '';
        }else{
            if((diffStart <= 0 && timeEndToStart <= 0) || (timeStartToEnd > 0 && timeEndToEnd > 0)){
                // 起止时间都小于当前班次的开始时间 或者 起止时间都大于当前班次结束时间
                resData[i].sDays = '';
            }else if(diffStart < 0 && timeEndToStart > 0){
                // 开始时间小于当前班次开始时间  结束时间大于当前班次开始时间
                //     if(i <= diffEnd){
                //         var diffDay = parseInt(Math.abs(diffStart)/1000/60/60/24,10) - 1;
                //         // 为月时天数需减一
                //         if(units === 3){
                //             diffDay = diffDay - 1 < 0 ? 0 : diffDay - 1;
                //         }
                //         resData[i].sDays = resData[i].sDays + diffDay;
                //         // resData[i].startTime = new Date(getDateStr(resData[i].startTime,resData[i].sDays) + ' 00:00:00').getTime()
                //     }else{
                //         resData[i].sDays = '';
                //     }
            }else if(diffStart === 0  && timeEndToStart >= 0){
                if(i <= diffEnd){
                    var diffDay = parseInt(Math.abs(diffStart)/1000/60/60/24,10);
                    // 为月时天数需减一
                    if(units === 3){
                        diffDay = diffDay - 1 < 0 ? 0 : diffDay - 1;
                    }
                    resData[i].sDays = resData[i].sDays + diffDay;
                    // resData[i].startTime = new Date(getDateStr(resData[i].startTime,resData[i].sDays) + ' 00:00:00').getTime()
                }else{
                    resData[i].sDays = '';
                }
            }
        }


        // 将startTime重构
        if(resData[i].sDays !== '' && yData[resData[i].sDays]){
            resData[i].startTime = new Date(yData[resData[i].sDays].split(' ')[0] + ' 00:00:00').getTime();
            resData[i].endTime = new Date(yData[resData[i].sDays].split(' ')[0] + ' 00:00:00').getTime();
        }


    }
    for (var i = 0; i < resData.length; i++) {
        //为周排班时
        if(units === 1){
            // 判断sDays 与yData的星期是否相等
            // sDays 0:星期一 1:星期二  以此类推
            for(var h = 0; h < yData.length; h++){
                var item = yData[h].split(' ');
                var week = item[1];
                var dateYData = item[0];
                var timeY = new Date(dateYData + ' 00:00:00').getTime();
                if( timeY >= timeRunStart && timeY <= timeRunEnd){
                    if(week === getDay1(resData[i].sDays%7) && sDaysList.indexOf(week) > -1){
                        resData[i].startTime = timeY;
                        resData[i].sDays = h;
                        resData[i].endTime = timeY;
                        break;
                    }
                }
            }
        }
    }
    // 判断所选时间段范围内包含几个单位为周的周期
    var startTimeStampStr = new Date(runStartTime.split(' ')[0]+' 00:00:00').getTime();
    var endTimeStampStr = new Date(runEndTime.split(' ')[0]+' 00:00:00').getTime();
    var dayCount = (endTimeStampStr - startTimeStampStr)/1000/60/60/24 + 1;//得到天数
    var newData = resData.slice(0);
    var cycleNum = dayCount/cycle;
    if(units === 1){
        var unitsDays = 7;
    }else if(units === 2){
        var unitsDays = cycle;
    }else if(units === 3){
        var unitsDays = 30;
    }
    if(parseInt(units,10) === 1 || parseInt(units,10) === 2 || parseInt(units,10) === 3) {
        //判断天数差是否大于一个循环且周期单位为月或天或周

        for (var t = 1; t < cycleNum; t++) {
            for (var k in newData) {
                if (newData[k].deilType === 1) {
                    var days = parseInt(newData[k].sDays, 10) + t * unitsDays;
                    resData.push({
                        deilType: newData[k].deilType,
                        schStartTime: newData[k].schStartTime,
                        schEndTime: newData[k].schEndTime,
                        sDays: days <= diffEnd ?  days: '',
                        startTime: days <= diffEnd ? new Date(yData[days].split(' ')[0] + ' 00:00:00').getTime():''
                    })
                }

                if(diffStart < 0  && timeEndToEnd <= 0){
                    // 开始时间小于当前班次开始时间 且结束时间小于当前班次结束时间
                    if(!(resData[k].startTime >= time2 && resData[k].startTime <= time3)){
                        resData[k].sDays = '';
                    }
                }
            }
        }
    }


    // 人员排班时  不需要跨天
    if(arguments.length === 5){
        // 判断是否跨天
        for(var j in resData) {
            var start = resData[j].schStartTime;
            var end = resData[j].schEndTime;
            var startTimeStr = parseInt(start.split(' ')[1].split(':')[0], 10);
            var endTimeStr = parseInt(end.split(' ')[1].split(':')[0], 10);
            if (startTimeStr > endTimeStr) {
                // isTwoTime = true;
                resData.push({
                    deilType: resData[j].deilType,
                    schStartTime: start,
                    schEndTime: start.split(' ')[0] + ' 24:00:00.0',
                    sDays: resData[j].sDays,
                    startTime: resData[j].sDays && yData[resData[j].sDays] ? new Date(yData[resData[j].sDays].split(' ')[0] + ' 00:00:00').getTime() : ''
                })

                var date2 = getDate1(endTime.split(' ')[0]);
                date2.setDate(date2.getDate1() + 1);
                var date2Day = date2.getFullYear() + "-" + getzf1(date2.getMonth() + 1) + "-" + getzf1(date2.getDate1());
                // 如果跨天超过查看时间的起止时间的结束日期  增加一天
                if(yData[resData[j].sDays] === yData[yData.length - 1]){
                    yData[yData.length] = date2Day + ' '+getMyDay1(date2Day);
                }
                resData.push({
                    deilType: resData[j].deilType,
                    schStartTime: start.split(' ')[0] + ' 00:00:00.0',
                    schEndTime: end,
                    sDays: parseInt(resData[j].sDays, 10) + 1,
                    startTime: resData[j].sDays && yData[resData[j].sDays] ? new Date(yData[parseInt(resData[j].sDays, 10) + 1].split(' ')[0] + ' 00:00:00').getTime() : ''
                })
            }
        }
    }

    for(var i = 0; i < resData.length; i++){
        if(resData[i].sDays === '' || resData[i].startTime === '' || isNaN(parseInt(resData[i].sDays)) || isNaN(parseInt(resData[i].startTime)) || resData[i].needDelete){
            resData.splice(i,1);
            i--;
        }
    }


    for(var k = 0; k < resData.length; k++){
        if(diffStart < 0  && timeEndToEnd <= 0){
            // 开始时间小于当前班次开始时间 且结束时间小于当前班次结束时间
            if(!(resData[k].startTime >= time2 && resData[k].startTime <= time3)){
                resData[k].sDays = '';
            }
        }else if(diffStart > 0 && timeStartToEnd <= 0 && timeEndToEnd >= 0){
            // 开始时间大于当前班次开始时间且小于当前班次结束时间 且结束时间大于当前班次结束时间
            // resData[i].sDays = resData[i].sDays;
            if(resData[k].startTime < time1 || resData[k].startTime > time4){
                resData[k].sDays = '';
            }
        }
    }

    var newResData = resData;
    for(var i = 0; i < newResData.length; i++){
        if(newResData[i].sDays === '' || newResData[i].startTime === '' || isNaN(parseInt(newResData[i].sDays)) || isNaN(parseInt(newResData[i].startTime)) || newResData[i].needDelete){
            newResData.splice(i,1);
            i--;
        }
    }
    resData = unCommon1(newResData);
    resData.sort(compare1('sDays'));
    var timeArr = [];//时间数组
    for(var j = 0;j < resData.length; j ++){
        var time = resData[j].startTime;
        var standardTime = formatTime1(time);
        standardTime = standardTime.substr(8,2);
        timeArr.push(standardTime);
    }
    return timeArr.toString();
}

//计算开始时间和结束时间相隔天数  周期单位为周时需携带星期几
function formatterDayByDay1(startTime, endTime) {
    startTime = startTime.substr(0,10);
    endTime = endTime.substr(0,10);
    console.log(startTime, endTime)
    var date1 = getDate1(startTime);
    var date2 = getDate1(endTime);
    if (date1 > date2) {
        var tempDate = date1;
        date1 = date2;
        date2 = tempDate;
    }
    date1.setDate(date1.getDate() + 1);
    var id = 1;
    var units = arguments[2];
    //为周排班时  日期携带星期
    var week = getMyDay1(startTime);
    var dates = [startTime + ' '+week];
    while (!(date1.getFullYear() == date2.getFullYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate())) {
        var yearMonthDay = date1.getFullYear() + "-" + getzf1(date1.getMonth() + 1) + "-" + getzf1(date1.getDate());
        var weekDay = getMyDay1(yearMonthDay);
        dates[id] = yearMonthDay + ' '+weekDay;
        id++;
        date1.setDate(date1.getDate() + 1);
    }

    if(arguments.length === 2){
        // 格式化临时排班中的时间
        dates[dates.length] = endTime + ' '+getMyDay1(endTime);

        formatterTemploryYData1(dates);
    }
    return dates;
}

function getDate1(str) {
    var tempDate = new Date();
    var list = str.split("-");
    tempDate.setFullYear(list[0]);
    tempDate.setMonth(list[1] - 1);
    tempDate.setDate(list[2]);
    return tempDate;
}

// 格式化临时排班中的添加时段
function formatterTemploryYData1(dateArr) {
    var obj = {};
    for (var i = 0; i < dateArr.length; i++) {
        obj[i] = dateArr[i];
    }
    localStorage.setItem('unitsDate', JSON.stringify(obj));
}

function getzf1(num) {
    if (parseInt(num) < 10) {
        num = '0' + num;
    }
    return num;
}
//获取指定日期为星期几
function getMyDay1(dateStr){
    var week;
    var date = new Date(dateStr);
    if(date.getDay()==0) week="日"
    if(date.getDay()==1) week="一"
    if(date.getDay()==2) week="二"
    if(date.getDay()==3) week="三"
    if(date.getDay()==4) week="四"
    if(date.getDay()==5) week="五"
    if(date.getDay()==6) week="六"
    return week;
}

function getDay1(day){
    var week;
    switch(day){
        case 0:
            week = '一';
            break;
        case 1:
            week = '二';
            break;
        case 2:
            week = '三';
            break;
        case 3:
            week = '四';
            break;
        case 4:
            week = '五';
            break;
        case 5:
            week = '六';
            break;
        case 6:
            week = '日';
            break;
        default:
            break;
    }
    return week;
}


// 数组对象去重
function unCommon1(obj){
    var uniques = [];
    var stringify = {};
    for (var i = 0; i < obj.length; i++) {
        var keys = Object.keys(obj[i]);
        keys.sort(function(a, b) {
            return (Number(a) - Number(b));
        });
        var str = '';
        for (var j = 0; j < keys.length; j++) {
            str += JSON.stringify(keys[j]);
            str += JSON.stringify(obj[i][keys[j]]);
        }
        if (!stringify.hasOwnProperty(str)) {
            uniques.push(obj[i]);
            stringify[str] = true;
        }
    }
    return uniques;
}
// 对象数组按指定项排序
function compare1(property){
    return function(a,b){
        var value1 = a[property];
        var value2 = b[property];
        return value1 - value2;
    }
}

function formatTime1(timeStamp)
{
    if(!timeStamp){
        return '';
    }
    var time = new Date(timeStamp);
    var y = time.getFullYear();
    var m = time.getMonth()+1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y+'-'+add00(m)+'-'+add00(d)+' '+add00(h)+':'+add00(mm)+':'+add00(s);
}

function add00(m){return m<10?'0'+m:m }